package com.taptech.doufu.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.fastjson.JSONObject
import com.taptech.doufu.R
import com.taptech.doufu.constant.Constant
import com.taptech.doufu.event.BaseEventBusBean
import com.taptech.doufu.event.EventBusConstant
import com.taptech.doufu.model.user.NovelBean
import com.taptech.doufu.services.personalcenter.AccountService
import com.taptech.doufu.ui.activity.NovelDetailsActivity
import com.taptech.doufu.util.GlideUtil
import com.taptech.doufu.util.TagsUtil
import com.taptech.doufu.weex.http.TFHttpError
import com.taptech.doufu.weex.http.TFHttpResponse
import com.taptech.doufu.weex.http.TFHttpUtil
import kotlinx.android.synthetic.main.adapter_user_detail_novel_list.view.*
import org.greenrobot.eventbus.EventBus

class UserDetailNovelListAdapter(context: Context,val uid:Int,val onRefreshDataListener: OnRefreshDataListener):BaseRecyclerAdappter<NovelBean,UserDetailNovelListAdapter.ViewHolder>(context) {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(mContext).inflate(R.layout.adapter_user_detail_novel_list,null)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        val item = dataList[position]
        GlideUtil.displayRoundImage(mContext, holder.itemView.ivCover, item.with_image_w, 5,R.drawable.img_default_loading_l)
        holder.itemView.tvTitle.text = item.topic_title
        holder.itemView.tvDesc.text = item.description
        holder.itemView.tvReadCount.text = item.tipTxt
        if (item.tags.size > 0) {
            holder.itemView.tvTag1.visibility = View.VISIBLE
            holder.itemView.tvTag1.text = item.tags[0].name
            holder.itemView.tvTag1.setOnClickListener {
                TagsUtil.openTagActivity(mContext, item.tags[0].name, Constant.OBJECT_TYPE_NOVEL)
            }
        }else{
            holder.itemView.tvTag1.visibility = View.GONE
        }
        if (item.pre_collect > 0) {
            holder.itemView.tvReadCount.visibility = View.GONE
            holder.itemView.tvPreCollect.visibility = View.VISIBLE
        }else{
            holder.itemView.tvPreCollect.visibility = View.GONE
            holder.itemView.tvReadCount.visibility = View.VISIBLE
        }
        if (AccountService.getInstance().isLogin && AccountService.getInstance().userUid.toInt() == uid) {
            holder.itemView.ivSetTop.visibility = View.VISIBLE
            if (item.is_top > 0) {
                holder.itemView.ivSetTop.setImageResource(R.drawable.icon_set_top_select)
                holder.itemView.ivSetTop.setOnClickListener {
                    cancelNovelTop(item.id)
                }
            }else{
                holder.itemView.ivSetTop.setImageResource(R.drawable.icon_set_top)
                holder.itemView.ivSetTop.setOnClickListener {
                    setNovelTop(item.id)
                }
            }
        }else{
            holder.itemView.ivSetTop.visibility = View.GONE
        }
        if (item.tags.size > 1) {
            holder.itemView.tvTag2.visibility = View.VISIBLE
            holder.itemView.tvTag2.text = item.tags[1].name
            holder.itemView.tvTag2.setOnClickListener {
                TagsUtil.openTagActivity(mContext, item.tags[1].name, Constant.OBJECT_TYPE_NOVEL)
            }
        }else{
            holder.itemView.tvTag2.visibility = View.GONE
        }
        holder.itemView.setOnClickListener {
            NovelDetailsActivity.startActivity(mContext,item.id)
        }
    }

    private fun setNovelTop(novelId:Int) {
        val map = HashMap<String, Any>()
        map["novel_id"] = novelId
        TFHttpUtil.requestGet("http://api.doufu.la/user/author/toppingNovel",map) { json, error ->
            val status = json.getInteger("status")
            if (status == 0) {
                val data = json.getJSONObject("data")
                if (data.containsKey("affected") && data.getInteger("affected") > 0) {
                    onRefreshDataListener.update()
                    Toast.makeText(mContext,"置顶成功",Toast.LENGTH_LONG).show()
                }else{
                    Toast.makeText(mContext,"置顶失败，请重试",Toast.LENGTH_LONG).show()
                }
            }else{
                Toast.makeText(mContext,"置顶失败，请重试",Toast.LENGTH_LONG).show()
            }
        }
    }

    private fun cancelNovelTop(novelId: Int) {
        val map = HashMap<String, Any>()
        map["novel_id"] = novelId
        TFHttpUtil.requestGet("http://api.doufu.la/user/author/cancelToppingNovel",map) { json, error ->
            val status = json.getInteger("status")
            if (status == 0) {
                val data = json.getJSONObject("data")
                if (data.containsKey("affected") && data.getInteger("affected") > 0) {
                    onRefreshDataListener.update()
                    Toast.makeText(mContext,"取消成功",Toast.LENGTH_LONG).show()
                }else{
                    Toast.makeText(mContext,"取消失败，请重试",Toast.LENGTH_LONG).show()
                }
            }else{
                Toast.makeText(mContext,"取消失败，请重试",Toast.LENGTH_LONG).show()
            }
        }
    }

    interface OnRefreshDataListener{
        fun update()
    }

    class ViewHolder(view:View): RecyclerView.ViewHolder(view)
}